package org.rainbowinverted.charging.device.connect.ykc.server.handler;

import org.rainbowinverted.charging.device.connect.ykc.server.message.in.YkcDeviceRebootIn;
import org.rainbowinverted.charging.device.connect.ykc.server.message.in.YkcMessageIn;
import org.rainbowinverted.charging.device.connect.ykc.server.message.out.YkcDeviceRebootOut;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 设备重启处理器
 * @author hehuaiping
 */
@Slf4j
@Component
@ChannelHandler.Sharable
public class YkcDeviceRebootHandler extends YkcAbstractHandler<YkcDeviceRebootIn, YkcDeviceRebootOut> {
    /**
     * @param channel channel
     * @param message message
     */
    @Override
    public void read(ChannelHandlerContext channel, YkcMessageIn message) {
        ByteBuf messageBody = message.getMessageBody();
        Long deviceNo = readDeviceNo(messageBody);
        // 执行控制结果
        boolean nowReboot = messageBody.readByte() == 0x01;
        log.info("reboot command result: {}", YkcDeviceRebootIn.of(deviceNo, nowReboot));
    }

    /**
     * @param byteBuf byteBuf
     * @param out     out
     */
    @Override
    public void write(ByteBuf byteBuf, YkcDeviceRebootOut out) {
        writeDeviceNo(byteBuf, out.getDeviceNo());
        // 执行控制
        byteBuf.writeByte(out.getNowReboot() ? 0x01 : 0x02);
    }
}
